package com.my.hzb.user.ctrl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.framelib.utils.Page;
import com.my.hzb.common.bean.GlobalBean;
import com.my.hzb.common.bean.LoginUserBean;
import com.my.hzb.common.bean.ResultBean;
import com.my.hzb.common.util.CommonUtil;
import com.my.hzb.common.util.HttpUtil;
import com.my.hzb.common.util.JsonUtil;
import com.my.hzb.common.util.MemcachedUtil;
import com.my.hzb.entiy.Address;
import com.my.hzb.entiy.Orders;
import com.my.hzb.entiy.UserInfo;
import com.my.hzb.entiy.UserMessage;
import com.my.hzb.user.service.AddressService;
import com.my.hzb.user.service.UserMessageService;
import com.my.hzb.user.service.UserService;

@Controller
@RequestMapping("/user")
public class LoginCtrl {

	private static final Logger log = LogManager.getLogger(LoginCtrl.class);
	@Autowired
	private UserService userService;
	@Autowired
	private AddressService addressService;
	@Autowired
	private UserMessageService userMessageService;
	@Autowired
	private MemcachedUtil memcachedUtil;
	
	@RequestMapping("/valideUserName")
	@ResponseBody
	public ResultBean valideUserName(UserInfo user){
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		if(CommonUtil.isNull(user.getUserName()))
		{
			//返回
			result.setRespMsg("请输入用户名");
			return result;
		}
		user.setStatus(GlobalBean.OK);
		List<UserInfo> list = userService.find(user,null);
		if(null!=list&&list.size()>0){
			//已经存在
			result.setRespMsg("用户已经存在");
			return result;
		}
		result.setRespCode(ResultBean.RETURN_SUCCESS_CODE);
		result.setRespMsg("OK");
		return result;
	}
	@RequestMapping("/reginer")
	@ResponseBody
	public ResultBean regin(HttpServletResponse response,UserInfo user){
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		if(CommonUtil.isNull(user.getUserName())||CommonUtil.isNull(user.getUserPwd()))
		{
			//返回
			return result;
		}
		List<UserInfo> list = userService.find(user,null);
		if(null!=list&&list.size()>0){
			//已经存在了
			//返回
			result.setRespMsg("该用户已经存在");
			return result;
		}
		user.setStatus(GlobalBean.OK);
		user.setUserPwd(CommonUtil.getMD5(user.getUserPwd()));
		userService.add(user);
		setCache(response,user);
		result.setRespCode(ResultBean.RETURN_SUCCESS_CODE);
		result.setRespMsg(ResultBean.RETURN_SUCCESS_MSG);
		result.setReturnObj(getResUrl(user.getUserName()));//"http://localhost:8080/hzb_order/toAddOrder");
//		result.setCookie((String)memcachedUtil.get("login_"+user.getUserName()));
		return result;
	}
	
	@RequestMapping("/login")
	@ResponseBody
	public ResultBean login(HttpServletResponse response,UserInfo user){
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		try{
			if(CommonUtil.isNull(user.getUserName())||CommonUtil.isNull(user.getUserPwd()))
			{
				//返回
				return result;
			}
			user.setStatus(GlobalBean.OK);
			user.setUserPwd(CommonUtil.getMD5(user.getUserPwd()));
			List<UserInfo> list = userService.find(user,null);
			if(null!=list&&list.size()>0){
	//			GlobalBean.loginCookie
				//缓存、cookie
				setCache(response,user);
			}else{
				result.setRespMsg("用户不存在");
				return result;
			}
			result.setRespCode(ResultBean.RETURN_SUCCESS_CODE);
			result.setRespMsg(ResultBean.RETURN_SUCCESS_MSG);
			result.setReturnObj(getResUrl(user.getUserName()));//"http://localhost:8080/hzb_order/toAddOrder");
//			result.setCookie((String)memcachedUtil.get("login_"+user.getUserName()));
		}catch(Exception e){
			log.error("登录失败",e);
		}
		return result;
	}
	public String getResUrl(String userName){
		Address ads = addressService.getDefByUser(userName);
		if(null!=ads){
			return GlobalBean.Url.order+"toAddOrder";
		}else{
			return "ads";
		}
	}
	public void setCache(HttpServletResponse response,UserInfo user){
		LoginUserBean userBean = new LoginUserBean();
		userBean.setCreateTime(new Date());
		userBean.setUserName(user.getUserName());
		userBean.setSessionId(UUID.randomUUID().toString().substring(0, 7));
		String userStr = JsonUtil.objectToJson(userBean);
		
		Cookie coo = new Cookie("hzb",userBean.getSessionId());
		coo.setPath("/");
		coo.setDomain(".www.houzhangbo.com");
		coo.setDomain(".pro.houzhangbo.com");
		coo.setDomain(".order.houzhangbo.com");
		response.addCookie(coo);
		memcachedUtil.set("login_"+user.getUserName(), 30000, userStr);
	}
	public static void main(String[] args) {
		String str = "eyJ1c2VyTmFtZSI6Imh6YiIsInNlc3Npb25JZCI6ImZkNzhlYTAyLWIyMjEtNDE2Zi1iMjBlLWNlZGEyYjZkMGFmNiIsImNyZWF0ZVRpbWUiOiIyMDE2LTA3LTE3IDIyOjI2OjQ2In0=";
		String json = CommonUtil.base64decode(str);
		LoginUserBean u = (LoginUserBean)JsonUtil.g.fromJson(json, LoginUserBean.class);
		System.out.println(u);
		System.out.println(u.getUserName());
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public ResultBean list(UserInfo user){
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		List<UserInfo> list = userService.find(user,null);
		if(null!=list){
			result.setReturnList(new ArrayList<Object>(list));
			result.setReturn(ResultBean.RETURN_SUCCESS_CODE, "ok");
		}
		return result;
	}
	@RequestMapping("/getMainTag")
	@ResponseBody
	public ResultBean getMainTag(String type){
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		List<Object> list = null;
		Page page = new Page();
		if("ads".equals(type)){
			Address ads = new Address();
			ads.setUserName("hzb");
			List<Address> list1 = addressService.find(ads,page);
			list = new ArrayList<Object>(list1);
		}else if("order".equals(type)){
			Orders order = new Orders();
			order.setUserName("hzb");
			List<Orders> list1 = new ArrayList<Orders>();
			list = new ArrayList<Object>(list1);
		}else if("msg".equals(type)){
			UserMessage uMsg = new UserMessage();
			uMsg.setUserName("hzb");
			List<UserMessage> list1 = userMessageService.find(uMsg,page);
			list = new ArrayList<Object>(list1);
		}
		if(null!=list){
			result.setReturnList(list);
			result.setReturn(ResultBean.RETURN_SUCCESS_CODE, "ok");
		}
		return result;
	}
}
